~ chicken-core (master) /manual/Using the interpreter


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Using the interpreter
  5
  6CHICKEN provides an interpreter named {{csi}} for evaluating Scheme programs
  7and expressions interactively.
  8
  9=== Writing Scheme scripts
 10
 11Since UNIX shells use the {{#!}} notation for starting scripts,
 12anything following the characters {{#!}} is ignored, with the exception of the special
 13symbols {{#!optional, #!key, #!rest, #!fold-case, #!no-fold-case}} and {{#!eof}}.
 14
 15The easiest way is to use the {{-script}} option like this:
 16
 17 % cat foo
 18 #! /usr/local/bin/csi -script
 19 (import (chicken port)
 20         (chicken process-context))
 21 (print (eval (with-input-from-string
 22                 (car (command-line-arguments))
 23                  read)))
 24
 25 % chmod +x foo
 26 % ./foo "(+ 3 4)"
 27 7
 28
 29The parameter {{command-line-arguments}} is set to a list of the
 30parameters that were passed to the Scheme script.  Scripts can be compiled
 31to standalone executables.
 32
 33Since it is sometimes useful to run a script in the interpreter without actually executing it
 34(for example to test specific parts of it), the option {{-ss}} can be used as an alternative to {{-script}}.
 35{{-ss PATHNAME}} is equivalent to {{-script PATHNAME}} but invokes {{(main (command-line-arguments))}}
 36after loading all top-level forms of the script file. The result of {{main}} is returned as the exit status
 37to the shell. Any non-numeric result exits with status zero:
 38
 39 % cat hi.scm
 40 (define (main args)
 41   (print "Hi, " (car args))
 42   0)
 43 % csi -ss hi.scm you
 44 Hi, you
 45 % csi -q
 46 #;1> ,l hi.scm
 47 #;2> (main (list "ye all"))
 48 Hi, ye all
 49 0
 50 #;3>
 51
 52When {{csi}} is started with the {{-script}} option, the feature identifier {{chicken-script}}
 53is defined, so can conditionally execute code depending on whether the file is
 54executed as a script or normally loaded into the interpreter, say for debugging purposes:
 55
 56<enscript highlight=scheme>
 57#!/bin/sh
 58#| demonstrates a slightly different way to run a script on UNIX systems
 59exec csi -s "$0" "$@"
 60|#
 61(import (chicken process-context))
 62
 63(define (main args) ...)
 64
 65(cond-expand
 66  (chicken-script
 67    (main (command-line-arguments)))
 68  (else))
 69</enscript>
 70
 71See also the documentation for the {{-ss}} option above.
 72
 73You can also have a look at [[/writing portable scripts]].
 74
 75
 76=== Toplevel commands
 77
 78The toplevel loop understands a number of special commands:
 79
 80; ,? : Show summary of available toplevel commands.
 81
 82; ,c : Show call-trace items of the most recent error
 83
 84; ,ch : Clears stored expression results of previously evaluated expressions.
 85
 86; ,d EXP : Describe result of evaluated expression {{EXP}}.
 87
 88; ,du EXP : Dump contents of the result of evaluated expression {{EXP}}.
 89
 90; ,dur EXP N : Dump {{N}} bytes of the result of evaluated expression {{EXP}}.
 91
 92; ,e FILENAME : Runs an external editor to edit the given {{FILENAME}} (see below for more information).
 93
 94; ,exn : Describes the last exception that occurred and adds it to the result history (it can be accessed using the {{#}} notation).
 95
 96; ,f N : Select call-trace item with the given number, where the number {{0}} indicates the last item in the trace
 97
 98; ,g NAME : Returns the value of the local variable with the given name (which may be a symbol or string); you don't have to give the complete name - {{,g}} will return the first variable that matches the prefix given
 99
100; ,h : Shows all previously evaluated expression results.
101
102; ,l FILENAME ... : Load files with given {{FILENAME}}s
103
104; ,ln FILENAME ... : Load files and print result(s) of each top-level expression.
105
106; ,m MODULENAME : switches the "current module" to {{MODULENAME}}, so expressions will be evaluated in the context of the given module.  To switch back to toplevel, use {{#f}} as a MODULENAME.  In compiled modules, only exported bindings will be visible to interactively entered code. In interpreted modules all bindings are visible.
107
108; ,p EXP : Pretty-print evaluated expression {{EXP}}.
109
110; ,q : Quit the interpreter.
111
112; ,r : Show system information.
113
114; ,s TEXT ... : Execute shell-command.
115
116; ,t EXP : Evaluate form and print elapsed time.
117
118; ,x EXP : Pretty-print macroexpanded expression {{EXP}} (the expression is not evaluated).
119
120; ,x1 EXP : Like {{,x}} but {{EXP}} is expanded only one step, using {{expand1}}.
121
122You can define your own toplevel commands using the {{toplevel-command}}
123procedure (see [[Module (chicken csi)]]).
124
125
126=== Getting error information
127
128Interpreted code has some extended debugging information available
129that can be used to locate errors and obtain information about the
130lexical environment that was effective at the point of error. When an
131error occurs in an evaluated expression, a "call trace" is printed -
132the list of calls up to the error location. Note that this does not
133follow a stack model: it is merely a list of recently made procedure
134calls where the last one in the list is (probably) the call of
135whatever procedure was executing before the error happened. You can
136use the {{,c}} command to show the call-trace of the last
137error. Depending on whether compiled or interpreted code was executing
138and how much debugging information is available, the call trace shows
139trace-buffer entries of the following shape:
140
141  <frame-number>:<environment?> <mode> <procedure-name> <form> 
142
143{{<frame-number>}} gives the number of the call-trace entry, counting
144from zero and beginning with the most recent entry. If a {{[]}}
145follows the frame-number, then this frame contains the lexical
146environment in effect when that procedure call took place. {{<mode>}}
147is optional and is either {{<syntax>}} or {{<eval>}} indicating
148whether this trace-buffer entry represents a syntax-expansion or an
149evaluation and is not given for compiled code. {{<form>}} is also only
150available for interpreted code and shows the procedure call
151expression, possibly following the name of the procedure containing
152the call expression.
153
154If the trace-buffer entry contains lexical environment information
155then the complete environment of the call site is shown.
156
157Use {{,f}} to select a frame by number, if you want to inspect the
158lexical environment of an earlier frame. The {{,g}} command lets you
159retrieve the value of a local or lexical variable from the currently
160selected frame. Note that the variables are renamed to simplify the
161variable lookup done internally by the interpreter.
162
163=== Running an external editor
164
165The {{,e}} command runs the editor given by:
166
167* The parameter {{editor-command}} in the {{(chicken csi)}} module should 
168  return a string naming
169  an external editor and defaults to {{#f}}, which means no editor is currently
170  selected (so the following alternatives are tried).
171
172* The contents of the environment variables {{EDITOR}} or {{VISUAL}}.
173
174* If the environment variable {{EMACS}} is set, the editor chosen is {{emacsclient}}.
175
176* In a desparate attempt to find an editor, {{vi}} is used.
177
178=== History access
179
180The interpreter toplevel accepts the special object {{#INDEX}} which
181returns the result of entry number {{INDEX}} in the history list. If
182the expression for that entry resulted in multiple values, the first
183result (or an unspecified value for no values) is returned. If no
184{{INDEX}} is given (and if a whitespace or closing paranthesis
185character follows the {{#}}, then the result of the last expression is
186returned.  Note that the value that {{#INDEX}} stands for is an expression,
187not a literal, and so is implicitly quoted, so
188
189 #;1> 123
190 123
191 #;2> '(1 2 #)
192
193will not return the result you expected.
194
195=== Auto-completion and editing
196
197On platforms that support it, it is possible to get auto-completion of
198symbols, history (over different {{csi}} sessions) and a more
199feature-full editor for the expressions you type using the
200[[/eggref/5/breadline|breadline]] egg by Vasilij Schneidermann.
201It is very useful for interactive use of csi. See the egg's
202documentation on how to set it up. If readline is not available on
203your system consider using the self-contained
204[[/eggref/5/linenoise|linenoise]] egg
205instead. It should work on almost any system but is not as
206feature-rich as readline (e.g. it lacks reverse-i-search and
207auto-completion).
208
209
210=== csi command line format
211
212{{csi {FILENAME|OPTION}}}
213
214where {{FILENAME}} specifies a file with Scheme source-code.  If the
215extension of the source file is {{.scm}}, it may be omitted. The
216runtime options described in [[Using the compiler#Compiler command line format|Compiler command line format]] are also available
217for the interpreter.  If the environment variable {{CSI_OPTIONS}}
218is set to a list of options, then these options are additionally passed
219to every direct or indirect invocation of {{csi}}. Please note that
220runtime options (like {{-:...}}) can not be passed using this method.
221The options recognized by the interpreter are:
222
223; -- : Ignore everything on the command-line following this marker. Runtime options ({{-:...}}) are still recognized.
224
225; -i  -case-insensitive : Enables the reader to read symbols case insensitive. The ddefault is to read case sensitive (as defined by R7RS).  This option registers the {{case-insensitive}} feature identifier.
226
227; -b  -batch : Quit the interpreter after processing all command line options.
228
229; -e  -eval EXPRESSIONS : Evaluate {{EXPRESSIONS}}. This option implies {{-batch}}, {{-no-init}} and {{-quiet}}, so no startup message will be printed and the interpreter exits after processing all {{-eval}} options and/or loading files given on the command-line.
230
231; -p  -print EXPRESSIONS : Evaluate {{EXPRESSIONS}} and print the results of each expression using {{print}}. Implies {{-batch}}, {{-no-init}} and {{-quiet}}.
232
233; -P  -pretty-print EXPRESSIONS : Evaluate {{EXPRESSIONS}} and print the results of each expression using {{pretty-print}}. Implies {{-batch}}, {{-no-init}} and {{-quiet}}.
234
235; -D  -feature SYMBOL : Registers {{SYMBOL}} to be a valid feature identifier for {{cond-expand}} and {{feature?}}.
236
237; -h  -help : Write a summary of the available command line options to standard output and exit.
238
239; -I  -include-path PATHNAME : Specifies an alternative search-path for files included via the {{include}} special form. This option may be given multiple times. If the environment variable {{CHICKEN_INCLUDE_PATH}} is set, it should contain a list of alternative include pathnames separated by {{:}} (UNIX) or {{;}} (Windows).
240
241; -K  -keyword-style STYLE : Enables alternative keyword syntax, where {{STYLE}} may be either {{prefix}} (as in Common Lisp) or {{suffix}} (as in DSSSL). Any other value is ignored.
242
243; -n  -no-init : Do not load initialization-file. If this option is not given and the file {{$HOME/.csirc}} exists, then it is loaded before the read-eval-print loop commences.
244
245;     -no-parentheses-synonyms : Disables list delimiter synonyms, [..] and {...} for (...).
246
247; -w  -no-warnings : Disables any warnings that might be issued by the reader or evaluated code.
248
249; -q  -quiet : Do not print a startup message. Also disables generation of call-trace information for interpreted code.
250
251;     -r7rs-syntax : Disables the CHICKEN extensions to R7RS syntax. Does not disable non-standard read syntax.
252
253; -s  -script PATHNAME : This is equivalent to {{-batch -quiet -no-init PATHNAME}}. Arguments following {{PATHNAME}} are available by using  {{command-line-arguments}} and are not processed as interpreter options. Extra options in the environment variable {{CSI_OPTIONS}} are ignored.
254
255; -sx PATHNAME : The same as {{-s PATHNAME}} but prints each expression to {{(current-error-port)}} before it is evaluated.
256
257; -ss PATHNAME : The same as {{-s PATHNAME}} but invokes the procedure {{main}} with the value of {{(command-line-arguments)}} as its single argument. If the main procedure returns an integer result, then the interpreter is terminated, returning the integer as the status code back to the invoking process. Any other result terminates the interpreter with a zero exit status.
258
259; -setup-mode : When locating extensions, search the current directory first. By default, extensions are located first in the ''extension repository'', where {{chicken-install}} stores compiled extensions and their associated metadata.
260
261; -R  -require-extension NAME : Equivalent to evaluating {{(import NAME)}}. {{NAME}} may be given in list notation, e.g. {{"(srfi 1)"}}.
262
263; -v  -version : Write the banner with version information to standard output and exit.
264
265
266---
267Previous: [[Getting started]]
268
269Next: [[Using the compiler]]
Trap